// 密钥对生成 http://web.chacuo.net/netrsakeypair
const publicKey = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdH\n' +
  'nzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ=='

const privateKey = 'MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqhHyZfSsYourNxaY\n' +
  '7Nt+PrgrxkiA50efORdI5U5lsW79MmFnusUA355oaSXcLhu5xxB38SMSyP2KvuKN\n' +
  'PuH3owIDAQABAkAfoiLyL+Z4lf4Myxk6xUDgLaWGximj20CUf+5BKKnlrK+Ed8gA\n' +
  'kM0HqoTt2UZwA5E2MzS4EI2gjfQhz5X28uqxAiEA3wNFxfrCZlSZHb0gn2zDpWow\n' +
  'cSxQAgiCstxGUoOqlW8CIQDDOerGKH5OmCJ4Z21v+F25WaHYPxCFMvwxpcw99Ecv\n' +
  'DQIgIdhDTIqD2jfYjPTY8Jj3EDGPbH2HHuffvflECt3Ek60CIQCFRlCkHpi7hthh\n' +
  'YhovyloRYsM+IS9h/0BzlEAuO0ktMQIgSPT3aFAgJYwKpqRYKlLDVcflZFCKY7u3\n' +
  'UP8iWi1Qw0Y='
// 加密
export function encrypt(txt) {
  const encryptor = new window.JSEncrypt();
  encryptor.setPublicKey(publicKey); // 设置公钥
  // 对数据进行解密
  if (txt.length >= 117) {
    return encryptor.encryptLong2(txt);
  } else {
    return encryptor.encrypt(txt);
  }
}

// 解密
export function decrypt(txt) {
  const encryptor = new window.JSEncrypt();
  encryptor.setPrivateKey(privateKey); // 设置私钥
  // 对数据进行解密
  if (txt.length >= 117) {
    return encryptor.decryptLong2(txt);
  } else {
    return encryptor.decrypt(txt);
  }
}

const encodeReserveRE = /[!'()*]/g;
const encodeReserveReplacer = (c) => '%' + c.charCodeAt(0).toString(16);
const commaRE = /%2C/g;

const encode = (str) => encodeURIComponent(str).replace(encodeReserveRE, encodeReserveReplacer).replace(commaRE, ',');

const decode = decodeURIComponent;

/**
* 判断字符串是否是base64
* @param { string } str
* @returns { boolean }
*/
function isBase64(str) {
 if (str === '' || str.trim() === '') {
   return false;
 }
 try {
   return btoa(atob(str)) == str;
 } catch (err) {
   return false;
 }
}

/**
* 序列化对象 并加密
* @param {Object} obj
*/
export const stringifyQuery = (obj) => {
 const res = obj
   ? Object.keys(obj)
       .map((key) => {
         const val = obj[key];

         if (val === undefined) {
           return '';
         }

         if (val === null) {
           return encode(key);
         }

         if (Array.isArray(val)) {
           const result = [];
           val.forEach((val2) => {
             if (val2 === undefined) {
               return;
             }
             if (val2 === null) {
               result.push(encode(key));
             } else {
               result.push(encode(key) + '=' + encode(val2));
             }
           });
           return result.join('&');
         }

         return encode(key) + '=' + encode(val);
       })
       .filter((x) => x.length > 0)
       .join('&')
   : null;
 return res ? `?${encrypt(res)}` : '';
};

/**
* 解密  反序列化字符串参数
* @param {String}} query
*/
export const parseQuery = (query) => {
 const res = {};

 query = query.trim().replace(/^(\?|#|&)/, '');

 if (!query) {
   return res;
 }

 // 解密
 query = isBase64(query) ? decrypt(query) : query;

 query.split('&').forEach((param) => {
   const parts = param.replace(/\+/g, ' ').split('=');
   const key = decode(parts.shift());
   const val = parts.length > 0 ? decode(parts.join('=')) : null;

   if (res[key] === undefined) {
     res[key] = val;
   } else if (Array.isArray(res[key])) {
     res[key].push(val);
   } else {
     res[key] = [res[key], val];
   }
 });
 console.log(res,'paser')
 return res;
};
